package com.dy.lock.screen.component.impl;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dy.lock.screen.component.MerchantInfoComponent;
import com.dy.lock.screen.constants.TransmissionCode;
import com.dy.lock.screen.exception.ServerDisponseException;
import com.dy.lock.screen.pojo.MerchantInfo;
import com.dy.lock.screen.scoket.Head;
import com.dy.lock.screen.service.MerchantService;
import com.dy.lock.screen.utils.CommonUtil;
import com.dy.lock.screen.vo.MerchantVo;
import com.google.gson.Gson;

@Component("merchantInfoComponent")
public class MerchantInfoComponentImpl implements MerchantInfoComponent {
	private static final Logger log = Logger
			.getLogger(MerchantInfoComponentImpl.class);
	private Gson gson;

	@Resource(name = "merchantService")
	private MerchantService merchantService;

	public MerchantInfoComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public MerchantVo getMerchantDetail(String body, Head head) {
		MerchantVo vo = gson.fromJson(body, MerchantVo.class);
		if (vo != null && StringUtils.isNotBlank(vo.getMerchantId())) {
			MerchantInfo merchantInfo = this.merchantService
					.queryByPrimaryKey(vo.getMerchantId());
			if (merchantInfo != null) {
				if (StringUtils.isNotBlank(merchantInfo.getCompanyLogo())) {
					vo.setPortrait(CommonUtil.getUrlPrefix()
							+ merchantInfo.getCompanyLogo());
				}
				vo.setName(merchantInfo.getCompanyName());
				vo.setBrief(merchantInfo.getCompanyAbstract());
				vo.setTel(merchantInfo.getCompanyTelNo());
				vo.setAddress(merchantInfo.getCompanyAddress());
				vo.setWebsite(merchantInfo.getCompanyWebsite());
			}
			return vo;
		} else {
			log.info("requestId:" + head.getRequestId()
					+ ",获取商户详情时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
